package theTVDB.com.command
{
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.http.HTTPService;
	
	import theTVDB.com.event.GetDefaultEpisodeEvent;
	import theTVDB.com.service.Services;
	import theTVDB.com.vo.EpisodeVO;

	public class GetDefaultEpisodeCommand extends CallbackCommand
	{
		private var services:Services = Services.getInstance() as Services;
		
		protected var seriesID:String;
		protected var season:int;
		protected var episode:int;
		
		override public function result(data:Object):void
		{
			var results:XML = (data.result as XML);
			var episodeData:XML;
			for each (episodeData in results.Episode)
			{
				var episodeVO:EpisodeVO = new EpisodeVO();
				episodeVO.id = episodeData.id;
				episodeVO.DVD_chapter = episodeData.DVD_chapter;
				episodeVO.DVD_discid = episodeData.DVD_discid;
				episodeVO.DVD_episodenumber = episodeData.DVD_episodenumber;
				episodeVO.DVD_season = episodeData.DVD_season;
				episodeVO.Director = episodeData.Director;
				episodeVO.EpisodeName = episodeData.EpisodeName;
				episodeVO.EpisodeNumber = episodeData.EpisodeNumber;
				episodeVO.FirstAired = episodeData.FirstAired;
				episodeVO.GuestStars = episodeData.GuestStars;
				episodeVO.IMDB_ID = episodeData.IMDB_ID;
				episodeVO.Language = episodeData.Language;
				episodeVO.Overview = episodeData.Overview;
				episodeVO.ProductionCode = episodeData.ProductionCode;
				episodeVO.Rating = episodeData.Rating;
				episodeVO.SeasonNumber = episodeData.SeasonNumber;
				episodeVO.Writer = episodeData.Writer;
				episodeVO.absolute_number = episodeData.absolute_number;
				episodeVO.airsafter_season = episodeData.airsafter_season;
				episodeVO.airsbefore_episode = episodeData.airsbefore_episode;
				episodeVO.airsbefore_season = episodeData.airsbefore_season;
				episodeVO.filename = episodeData.filename;
				episodeVO.lastupdated = episodeData.lastupdated;
				episodeVO.seasonid = episodeData.seasonid;
				episodeVO.seriesid = episodeData.seriesid;
			}

			if (callback != null)
				callback(episodeVO, seriesID, season, episode);
		}

		override public function fault(info:Object):void
		{
			trace(info);
			
			if (callback != null)
				callback(null, seriesID, season, episode);
		}

		public override function execute( event : CairngormEvent ) : void
		{
			var myEvent:GetDefaultEpisodeEvent = event as GetDefaultEpisodeEvent;
			callback = myEvent.callback;
			seriesID = myEvent.seriesID;
			season = myEvent.season;
			episode = myEvent.episode;
			var tmpService:HTTPService = services.getDefaultEpisode(myEvent.seriesID,myEvent.season, myEvent.episode);
			var token:AsyncToken = tmpService.send();
			token.addResponder(this);
		}
	}
}
